package com.inspur.sc.springboot.controller;

import com.inspur.sc.springboot.entity.SysPermission;
import com.inspur.sc.springboot.entity.SysUser;
import com.inspur.sc.springboot.service.SysPermissionService;
import com.inspur.sc.springboot.utils.MenuUtil;
import com.inspur.sc.springboot.utils.PerMessionTreeUtil;
import com.inspur.sc.springboot.utils.ResponseUtil;
import com.inspur.sc.springboot.utils.SpringSecurityUtils;
import com.inspur.sc.springboot.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/permission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService permissionService;

    @GetMapping("findAllSysPermission")
    public ResponseEntity<ResponseVO> findAllSysPermission(){
        List<SysPermission> permissions=permissionService.findAll();
        if(!permissions.isEmpty()){
            return ResponseUtil.success(PerMessionTreeUtil.getMenuJson(permissions,false));
        }
        return ResponseUtil.success(null);
    }

    @GetMapping("getPermission")
    public ResponseEntity<ResponseVO> getPermission(@RequestParam("id") Integer id){
        SysPermission sysPermission=permissionService.findById(id);
        return ResponseUtil.success(sysPermission);
    }

    @PostMapping("save")
    public ResponseEntity<ResponseVO> save(@RequestBody SysPermission permission){
        boolean isSave=permissionService.save(permission);
        if(isSave){
            return ResponseUtil.success("保存成功",true);
        }
        return ResponseUtil.success("保存失败",false);
    }

    @GetMapping("delete")
    public ResponseEntity<ResponseVO> delete(@RequestParam("id") Integer id){
        boolean isDel=permissionService.deleteById(id);
        if(isDel){
            return ResponseUtil.success("删除成功",true);
        }
        return ResponseUtil.success("删除失败",false);
    }

    @GetMapping("findSysPermissionByGroupId")
    public ResponseEntity<ResponseVO> findSysPermissionByGroupId(@RequestParam("id") Integer id){
        List<SysPermission> permissions=permissionService.findByGroupId(id);
        return ResponseUtil.success(permissions);
    }
}
